//
// Created by gj on 16-2-21.
//

#ifndef ALLUTILS_POINTERCOUNTER_H
#define ALLUTILS_POINTERCOUNTER_H

#include "../log/CLog.h"

#include <stdint.h>

class PointerCounter {
public:
    PointerCounter() //
            : LOG_INIT_NAME_DEBUG("PointerCounter")
            count(new int32_t(1)) {
        LOG_I("PointerCounter create.");
    }

    PointerCounter(PointerCounter &counter) //
            : LOG_INIT_NAME_DEBUG("PointerCounter")
            count(NULL) {
        copy(counter);
        LOG_I("PointerCounter create: count = %d.", getCount())
    }

    int32_t getCount() {
        return count == NULL ? 0 : *count;
    }

    int copy(PointerCounter &counter);

    int decCount();

    int incCount() {
        return (*count)++;
    }

    ~PointerCounter() {
        LOG_I("PointerCounter release: count = %p, %d.", count, getCount());
    }

private:
    LOG_CREATE_DEBUG

    int32_t *count;
};

// --------------------------------------------- unit test --------------------------------------------------
#ifdef FLAG_UNIT_TESTING

void pointer_counter_unit_testing();

#endif

#endif //ALLUTILS_POINTERCOUNTER_H
